<?php

namespace model;


class common
{
	private static function db()
	{
		return \libs\db::Init();
	}

	//查询用户组的ID 和 用户组名列表，此数据长期缓存
	protected static function getAdminGroup()
	{
		return static::db()->table('admin_group')->Field('id, groupname')->cache(false)->select();
	}

	//查询用户组的ID 和 用户组名列表，此数据长期缓存
	protected static function getAdminUser()
	{
		return static::db()->table('admin_user')->Field('id, nickname')->Cache(false)->select();
	}

	//查询所有栏目列表
	protected static function getCateData()
	{
		return static::db()->table('article_cate')->Cache(false)->Order('sort ASC')->Select();
	}

	//查询指定栏目列表
	public static function getCateList($id = null, $cache = 0, $order = 'sort ASC')
	{
		if (!empty($id)) {
			$where = "id IN (" . $id . ")";
			return static::db()->table('article_cate')->where($where)->Cache($cache)->Order($order)->Select();
		} else {
			return self::getCateData();
		}
	}

	//文章内容栏目路径显示，前端根据数据进行判断
	public static function getCateUrlData($cid)
	{
		$cate = new \common\Category(array('id', 'pid', 'name', 'cname'));
		return $cate->getPath(self::getCateData(), $cid); // 以升序对数组排序
	}

	//文章内容栏目子栏目数组
	public static function getCateChildData($cid)
	{
		$cate = new \common\Category();
		return $cate->getChild($cid, self::getCateData()); // 以升序对数组排序
	}

	//根据已经查询到的数组，查找指定的一条栏目数据
	public static function findCateData($cid)
	{
		$arr = ['name', 'theme', 'contheme', 'id', 'pid', 'num', 'urlname'];
		if (is_numeric($cid))
			return FindOneArrayBYFilter(self::getCateData(), 'id', $cid, $arr);
		else
			return FindOneArrayBYFilter(self::getCateData(), 'urlname', $cid, $arr);
	}

	//根据已经查询到的数组，查找指定的一条用户组数据
	public static function findAdminGroupData($gid)
	{
		$arr = ['groupname'];
		$result = FindOneArrayBYFilter(self::getAdminGroup(), 'id', $gid, $arr);
		return $result['groupname'];
	}


	public static function findAdminUserData($uid)
	{
		$arr = ['nickname'];
		$result = FindOneArrayBYFilter(self::getAdminUser(), 'id', $uid, $arr);
		return $result['nickname'];
	}

	//查询指定数据表的列表
	public static function get_List($data)
	{
		$table = $data['table'];
		switch ($table) {
			case 'article':
				return \model\article::selectData($data['cid'] ?? 0, $data['limit'] ?? 10, $data['cache'] ?? 600, $data['imgbl'] ?? 0, $data['attribute'] ?? 0, $data['order'] ?? "id desc");
				break;
			case 'article_cate':
				return self::getCateList($data['id'] ?? 0, $data['cache'] ?? 0, $data['order'] ?? "sort ASC");
				break;
			case 'images':
				return \model\images::selectData();
				break;
			case 'onduty':
				return \model\ondutys::findData();
				break;
			default:
				//通用列表调用方法  ： CxuuList("table='XXX' key='value' value='1' limit='10' cache='600' order='sort DESC'")
				if (!empty($data['key']))
					$where[$data['key']] = $data['value'];
				else
					$where = '';
				return static::db()->table($table)->where($where)->limit($data['limit'] ?? 10)->cache($data['cache'] ?? 0)->order($data['order'] ?? '')->select();
		}
	}
}
